﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Reflection;

namespace CodeStash.Common.Encryption
{
    public class EncryptionHelper
    {

        private static readonly string passPhrase = "Pas5pr@se";        // can be any string
        private static readonly string saltValue = "s@1tValue";         // can be any string
        private static readonly string hashAlgorithm = "SHA1";          // can be "MD5"
        private static readonly int passwordIterations = 2;             // can be any number
        private static readonly string initVector = "@1B2c3D4e5F6g7H8"; // must be 16 bytes
        private static readonly int keySize = 256;                      // can be 192 or 128
        private static readonly bool encryptionEnabled = false;

        /// <summary>
        /// Static constructor reads the EncryptionEnabled from the App.Config or Web.Config
        /// </summary>
        static EncryptionHelper()
        {
            encryptionEnabled = CodeStash.Common.Helpers.ConfigurationSettings.EncryptionEnabled;
        }

        /// <summary>
        /// Is encryption enabled, as dictated by the App.Config / Web.config 
        /// EncryptionEnabled AppSetting
        /// </summary>
        public static bool EncryptionEnabled
        {
            get { return encryptionEnabled; }
        }

        /// <summary>
        /// Encrypts a single value which is returned
        /// </summary>
        public static string GetEncryptedValue(string valueToEncrypt)
        {
            return RijndaelSimple.Encrypt(valueToEncrypt, passPhrase, saltValue, hashAlgorithm,
                passwordIterations, initVector, keySize);
        }


        public static string GetDecryptedValue(string valueToDecrypt)
        {
            return RijndaelSimple.Decrypt(valueToDecrypt, passPhrase, saltValue, hashAlgorithm,
                passwordIterations, initVector, keySize);
        }

        //private static string GetAsBase64String(string valueToConvert)
        //{
        //    BinaryFormatter bf = new BinaryFormatter();
        //    byte[] bytes;
        //    using(MemoryStream ms = new MemoryStream())
        //    {
        //        bf.Serialize(ms, valueToConvert);
        //        ms.Seek(0, 0);
        //        bytes = ms.ToArray();
        //    }
        //    return Convert.ToBase64String(bytes);
        //}

        /// <summary>
        /// Decrypts the 3 input values, and passes back a anonomous object with the 3
        /// decrypted values as the following 3 properties
        /// - DecryptedCodeStashToken
        /// - DecryptedEmail
        /// - DecryptedPassword
        /// </summary>
        public static DecryptedUserValues GetDecryptedValues(string codeStashToken, string email, string password)
        {
            string decryptedCodeStashToken = !string.IsNullOrEmpty(codeStashToken) && !string.Equals("\"\"",codeStashToken) ?
              RijndaelSimple.Decrypt(codeStashToken, passPhrase, saltValue, hashAlgorithm,
              passwordIterations, initVector, keySize) : "";

            string decryptedEmail = RijndaelSimple.Decrypt(email, passPhrase, saltValue, hashAlgorithm,
                passwordIterations, initVector, keySize);

            string decryptedPassword = !string.IsNullOrEmpty(password) && !string.Equals("\"\"", password) ?
                RijndaelSimple.Decrypt(password, passPhrase, saltValue, hashAlgorithm,
                passwordIterations, initVector, keySize) : "";

            return new DecryptedUserValues(decryptedCodeStashToken,decryptedEmail,decryptedPassword);
        }

    }
}
